package com.zmx.patientend.view.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.patientend.BaseActivity;
import com.zmx.patientend.ExpertTeamDoctorAdapter;
import com.zmx.patientend.R;
import com.zmx.patientend.adapter.EvaluationAdapter;
import com.zmx.patientend.adapter.OrderItemAdapter;
import com.zmx.patientend.okhttp.OkHttp3ClientManager;
import com.zmx.patientend.pojo.Doctor;
import com.zmx.patientend.pojo.Evaluation;
import com.zmx.patientend.pojo.ExpertTeamDoctor;
import com.zmx.patientend.pojo.PatientOrder;
import com.zmx.patientend.utils.AllUtil;
import com.zmx.patientend.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
  *
  *程序猿：胖胖祥
  *时 间：2020/11/17  23:24
  *功 能：专家团队详情
  *
  */

public class ExpertTeamDetailActivity extends BaseActivity {

    @BindView(R.id.team_name)
    TextView teamName;
    @BindView(R.id.team_expertise)
    TextView teamExpertise;
    @BindView(R.id.team_doctor_head)
    CircleImageView teamDoctorHead;
    @BindView(R.id.team_doctor_names)
    TextView teamDoctorNames;
    @BindView(R.id.team_doctor_title)
    TextView teamDoctorTitle;
    @BindView(R.id.team_doctor_depment)
    TextView teamDoctorDepment;
    @BindView(R.id.doctor_recycler)
    RecyclerView doctorRecycler;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.add_button)
    Button addButton;
    @BindView(R.id.button_layout)
    LinearLayout buttonLayout;
    @BindView(R.id.quiz)
    Button quiz;
    @BindView(R.id.no_data)
    TextView no_data;

    private String team_id;

    private ExpertTeamDoctorAdapter doctor_adapter;
    private List<ExpertTeamDoctor> doctors;

    private List<Evaluation> lists;
    private EvaluationAdapter adapter;
    private Doctor doctor;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_expert_team_detail;
    }

    @Override
    protected void initViews() {

        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);
        team_id = this.getIntent().getStringExtra("team_id");
        Log.e("专家团尽力啊","进来聊聊00000team_id "+team_id);
        doctor = (Doctor) this.getIntent().getSerializableExtra("doctor");

        doctorRecycler.setLayoutManager(new LinearLayoutManager(mActivity));

        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new EvaluationAdapter(mActivity);
        recyclerView.setAdapter(adapter);
        lists = new ArrayList<>();



        getData();
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 1:

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());

                        JSONObject info = object.getJSONObject("info");

                        teamName.setText(info.getString("name"));
                        teamExpertise.setText(info.getString("describe"));
                        doctor.setImgtextfee(info.getDouble("price"));

                        int logo = 0;

                        if (Integer.parseInt(MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, "")) == info.getInt("member_id")){

                            buttonLayout.setVisibility(View.VISIBLE);
                            logo = 1;

                        }else{

                            buttonLayout.setVisibility(View.GONE);
                            logo = 0;

                        }

                        doctor_adapter = new ExpertTeamDoctorAdapter(mActivity, logo);
                        doctorRecycler.setAdapter(doctor_adapter);
                        doctors = new ArrayList<>();

                        JSONArray user = object.getJSONArray("user");
                        Gson gson = new Gson();
                        for (int i = 0; i < user.length(); i++) {

                            JSONObject doctor = user.getJSONObject(i);


                            if (doctor.getInt("captain") == 1) {

                                teamDoctorNames.setText(doctor.getString("name"));
                                teamDoctorDepment.setText(doctor.getString("hosp_name") + " " + doctor.getString("class_name"));
                                teamDoctorTitle.setText(doctor.getString("title_name"));
                                Glide.with(mActivity).load(doctor.getString("avatar")).into(teamDoctorHead);

                            } else {

                                ExpertTeamDoctor td = gson.fromJson(doctor.toString(), ExpertTeamDoctor.class);
                                doctors.add(td);

                            }


                        }
                        doctor_adapter.setListAndNotifyDataSetChanged(doctors);

                        if (doctors.size()>0){

                            no_data.setVisibility(View.GONE);

                        }else {

                            no_data.setVisibility(View.VISIBLE);
                        }

                        JSONArray array = new JSONArray(object.getString("comments"));
                        for (int i = 0; i < array.length(); i++) {

                            JSONObject jsonObject = array.getJSONObject(i);
                            Evaluation order = gson.fromJson(jsonObject.toString(), Evaluation.class);
                            lists.add(order);

                        }

                        adapter.setListAndNotifyDataSetChanged(lists);//就是设置list然后刷新

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                    break;

            }

        }
    };

    public void getData() {

        List<String> ls = new ArrayList<>();
        ls.add(team_id);
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "teamHome");
        params.put("data[team_id]", team_id);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/admin/api.team/entry", params, handler, 1, 404);

    }

    @OnClick({R.id.add_button, R.id.quiz})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.add_button:

                break;

            case R.id.quiz:
                pop_up();
                break;

        }
    }


    private Dialog modify_dialogs;//弹出框
    private int asktype = 0;//问诊类型
    public void pop_up() {

        View view;//选择性别的view
        RelativeLayout graphic, call_physician, special_video;

        modify_dialogs = new Dialog(mActivity, R.style.ActionSheetDialogStyle);
        //填充对话框的布局
        view = LayoutInflater.from(mActivity).inflate(R.layout.wenzhen, null);
        TextView members_number = view.findViewById(R.id.members_number);
        members_number.setText("￥" + doctor.getImgtextfee());
        TextView call = view.findViewById(R.id.call);
        call.setText("￥" + doctor.getCallfee());
        //将布局设置给Dialog
        modify_dialogs.setContentView(view);
        //获取当前Activity所在的窗体
        Window dialogWindow = modify_dialogs.getWindow();

        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        //        lp.y = 20;//设置Dialog距离底部的距离

        //// 以下这两句是为了保证按钮可以水平满屏
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        // 设置显示位置
        modify_dialogs.onWindowAttributesChanged(lp);
        //       将属性设置给窗体
//        modify_dialogs.setCanceledOnTouchOutside(false);
        modify_dialogs.show();//显示对话框

        graphic = view.findViewById(R.id.graphic);
        graphic.setOnClickListener(v -> {

            asktype = 1;
            Intent intent = new Intent(mActivity, GraphicVisitsActivity.class);
            intent.putExtra("doctor", doctor);
            intent.putExtra("asktype", asktype + "");
            intent.putExtra("class_id", 0 + "");
            intent.putExtra("team_id", team_id);
            startActivity(intent);
            modify_dialogs.dismiss();

        });

        call_physician = view.findViewById(R.id.call_physician);
        call_physician.setOnClickListener(v -> {

            asktype = 2;
            Intent intent = new Intent(mActivity, GraphicVisitsActivity.class);
            intent.putExtra("doctor", doctor);
            intent.putExtra("asktype", asktype + "");
            intent.putExtra("class_id", 0 + "");
            intent.putExtra("team_id", team_id);
            startActivity(intent);
            modify_dialogs.dismiss();

        });

        special_video = view.findViewById(R.id.special_video);
        special_video.setOnClickListener(v -> {

            asktype = 3;
            Intent intent = new Intent(mActivity, GraphicVisitsActivity.class);
            intent.putExtra("doctor", doctor);
            intent.putExtra("asktype", asktype + "");
            intent.putExtra("class_id", 0 + "");
            intent.putExtra("team_id", team_id);
            startActivity(intent);
            modify_dialogs.dismiss();

        });

    }

}
